/*******************************************
 *                                         *
 *******************************************/

#include "Sprite.h"

namespace DirectXFramework
{
	namespace Graphics
	{
		//Constructors
		Sprite::Sprite (void)
		{
			this->Quad = NULL;
			this->Texture = NULL;
		}

		Sprite::Sprite (LPDIRECT3DDEVICE9 DrawDevice, float Width, float Height, float Depth)
		{
			this->Quad = NULL;
			this->Texture = NULL;
			this->Init(DrawDevice, Width, Height, 0.5f);
		}

		//Destructor
		Sprite::~Sprite (void)
		{		
			if (this->Quad)
				delete[] Quad;
			if (this->Texture)
				this->Texture->Release();
		}

		//Public Methods
		BOOL Sprite::Init (LPDIRECT3DDEVICE9 DrawDevice, float SpriteWidth, float SpriteHeight, float SpriteDepth)
		{
			BOOL Success = TRUE;

			this->Quad = new SpritePoint[4];
			if (!this->Quad)
				Success = FALSE;
			else
			{
				XLocation = 0.0f;
				YLocation = 0.0f;
				Width = SpriteWidth;
				Height = SpriteHeight;
				Depth = SpriteDepth;
				this->Device = DrawDevice;
				this->CreateQuad();
			}

			return Success;
		}

		BOOL Sprite::LoadTexture (LPCTSTR File)
		{
			BOOL Success = TRUE;

			if (FAILED(D3DXCreateTextureFromFile(this->Device, File, &this->Texture)))
				Success = FALSE;

			return Success;
		}

		void Sprite::Draw (void)
		{
			this->Device->SetFVF(SPRITE_FVF);
			if (this->Texture)
				this->Device->SetTexture(0, Texture);
			this->Device->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, Quad, sizeof(SpritePoint));
		}

		//Setters
		void Sprite::SetLocation (float XLoc, float YLoc)
		{
			this->XLocation = XLoc;
			this->YLocation = YLoc;
			for (int i = 0; i < 4; i++)
			{
				this->Quad[i].X += this->XLocation;
				this->Quad[i].Y += this->YLocation;
			}
		}

		//Private Methods
		void Sprite::CreateQuad (void)
		{
			this->Quad[0] = SpritePoint(XLocation, YLocation, Depth, 1.0f, COLOR(255, 255, 255), 0.0f, 0.0f);
			this->Quad[1] = SpritePoint(XLocation + Width, YLocation, Depth, 1.0f, COLOR(255, 255, 255), 1.0f, 0.0f);
			this->Quad[2] = SpritePoint(XLocation + Width, YLocation + Height, Depth, 1.0f, COLOR(255, 255, 255), 1.0f, 1.0f);
			this->Quad[3] = SpritePoint(XLocation, YLocation + Height, Depth, 1.0f, COLOR(255, 255, 255), 0.0f, 1.0f);
		}
	};
};